﻿using System;
using System.Windows.Input;

namespace Blackbird.Studio.Mvvm
{
    public class RelayCommand : ICommand
    {
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _execute;

        public RelayCommand(Action<object> execute) : this(execute, null)
        {
        }

        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException();
            _execute = execute;
            _canExecute = canExecute;
        }

        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return _canExecute == null || _canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        #endregion
    }

    public class RelayCommand<TExecute> : ICommand {
        private readonly Predicate<object> _canExecute;
        private readonly Action<TExecute> _execute;

        public RelayCommand(Action<TExecute> execute)
            : this(execute, null)
        {

        }

        public RelayCommand(Action<TExecute> execute, Predicate<object> canExecute) {
            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter) {
            return _canExecute == null || _canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter) {
            _execute((TExecute)parameter);
        }
    }

    public class RelayCommand<TExecute, TCanExecute> : ICommand
    {
        private readonly Predicate<TCanExecute> _canExecute;
        private readonly Action<TExecute> _execute;
        public RelayCommand(Action<TExecute> execute)
            : this(execute, null)
        {

        }
        
        public RelayCommand(Action<TExecute> execute, Predicate<TCanExecute> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;

        }

        public bool CanExecute(object parameter)
        {
            return _canExecute == null || _canExecute((TCanExecute)parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            _execute((TExecute)parameter);
        }
    }

}